package com.zsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.constant.MessageConstant;
import com.zsc.context.BaseContext;
import com.zsc.dto.ShoppingCartDTO;
import com.zsc.dto.ShoppingCartPageQueryDTO;
import com.zsc.entity.Dish;
import com.zsc.entity.Setmeal;
import com.zsc.entity.ShoppingCart;
import com.zsc.exception.ShoppingCartSubDishOrSetmealException;
import com.zsc.mapper.DishMapper;
import com.zsc.mapper.SetmealMapper;
import com.zsc.mapper.ShoppingCartMapper;
import com.zsc.result.PageResult;
import com.zsc.service.ShoppingCartService;
import com.zsc.utils.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cjr165123050
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2024-05-19 11:01:52
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查看购物车
     *
     * @return
     */
//    @Override
//    public List<ShoppingCart> showShoppingCart() {
//        Long userId = BaseContext.getCurrentId();
//
//        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id", userId);
//
//        return this.list(queryWrapper);
//    }
    @Override
    public PageResult showShoppingCart(ShoppingCartPageQueryDTO shoppingCartPageQueryDTO) {
        Long userId = BaseContext.getCurrentId();

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        Page<ShoppingCart> pageInfo = new Page<>(shoppingCartPageQueryDTO.getPage(), shoppingCartPageQueryDTO.getPageSize());
        this.page(pageInfo, queryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRecords(pageInfo.getRecords());

        return pageResult;
    }

    /**
     * 新增/添加 菜品/套餐 至购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = CopyUtil.copy(shoppingCartDTO, ShoppingCart.class);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        // 判断当前加入购物车的商品是否存在（根据userId和setmealId或dishId来查）
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq(shoppingCartDTO.getDishId() != null, "dish_id", shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null, "setmeal_id", shoppingCartDTO.getSetmealId());

        ShoppingCart existingCart = this.getOne(queryWrapper);

        if (existingCart != null) {
            // 存在则商品数量加一
            existingCart.setNumber(existingCart.getNumber() + 1);
            this.updateById(existingCart);
            return;
        }

        Long dishId = shoppingCartDTO.getDishId();
        // 不存在则插入一条购物车数据
        if (dishId != null) {
            // 本次添加到购物车的是菜品
            Dish dish = dishMapper.selectById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
        } else {
            // 本次添加到购物车的是套餐
            Long setmealId = shoppingCartDTO.getSetmealId();
            Setmeal setmeal = setmealMapper.selectById(setmealId);
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
        }

        shoppingCart.setNumber(1);
//        shoppingCart.setCreateTime(LocalDateTime.now());
        this.save(shoppingCart);

    }

    /**
     * 减少/删除 菜品/套餐 至购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq(shoppingCartDTO.getDishId() != null, "dish_id", shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null, "setmeal_id", shoppingCartDTO.getSetmealId());

        ShoppingCart cart = this.getOne(queryWrapper);

        if (cart != null) {
            if (cart.getNumber() > 1) {
                cart.setNumber(cart.getNumber() - 1);
                this.updateById(cart);
            } else {
                this.removeById(cart.getId());
            }
        } else {
            throw new ShoppingCartSubDishOrSetmealException(MessageConstant.DISH_OR_SETMEAL_NUMBER_IS_ZERO);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        this.remove(queryWrapper);
    }


}




